import { Navigate } from 'react-router-dom';
import useAuthStore from '../store/authStore';

interface AdminRouteProps {
  children: React.ReactNode;
}

const AdminRoute = ({ children }: AdminRouteProps) => {
  const { userInfo } = useAuthStore();

  // 如果用户已登录且角色是 'admin'，则允许访问
  if (userInfo && userInfo.role === 'admin') {
    return <>{children}</>;
  }

  // 否则，重定向到首页
  return <Navigate to="/" replace />;
};

export default AdminRoute;